package topuc.com.mvp.fragment;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import topuc.com.mvp.R;
import topuc.com.mvp.utils.EmotionKeyboard;
import topuc.com.mvp.utils.GlobalOnItemClickManagerUtils;


/**
 * 控制底部表情布局的Fragment
 */
public class EmotionFragment extends BaseFragment {

    //是否绑定当前Bar的编辑框的flag
    public static final String BIND_TO_EDITTEXT="bind_to_edittext";
    //是否隐藏bar上的编辑框和发送按钮
    public static final String HIDE_BAR_EDITTEXT_AND_BTN="hide bar's editText and btn";

    //是否绑定当前Bar的编辑框,默认true,即绑定。
    //false,则表示绑定contentView,此时外部提供的contentView必定也是EditText
    private boolean isBindToBarEditText=true;

    //是否隐藏bar上的编辑框和发生按钮,默认不隐藏
    private boolean isHidenBarEditTextAndBtn=false;

    private EmotionKeyboard mEmotionKeyboard;

    private View mContentView;
    private EditText bar_edit_text;

    private ImageView barImageAddBtn;
    private Button btnSend;
    private LinearLayout rlEditbarBg;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_emotion, container, false);

        isHidenBarEditTextAndBtn= args.getBoolean(EmotionFragment.HIDE_BAR_EDITTEXT_AND_BTN);
        //获取判断绑定对象的参数
        isBindToBarEditText=args.getBoolean(EmotionFragment.BIND_TO_EDITTEXT);
        initView(view);

        mEmotionKeyboard=EmotionKeyboard.with(getActivity())
                .setEmotionView(view.findViewById(R.id.ll_emotion_layout))
                .bindToContent(mContentView)
                .bindEditText(!isBindToBarEditText ? ((EditText) mContentView) : ((EditText) view.findViewById(R.id.bar_edit_text)))//判断绑定那种EditView
                .bindToEmotionButton(view.findViewById(R.id.emotion_button))
                .build();

        //创建全局监听
        GlobalOnItemClickManagerUtils globalOnItemClickManager= GlobalOnItemClickManagerUtils.getInstance(getActivity());

        if(isBindToBarEditText){
            //绑定当前Bar的编辑框
            globalOnItemClickManager.attachToEditText(bar_edit_text);

        }else{
            // false,则表示绑定contentView,此时外部提供的contentView必定也是EditText
            globalOnItemClickManager.attachToEditText((EditText) mContentView);
            mEmotionKeyboard.bindEditText((EditText)mContentView);
        }

        return view;
    }

    private void initView(View view) {

        barImageAddBtn=view.findViewById(R.id.bar_image_add_btn);
        btnSend=view.findViewById(R.id.bar_btn_send);
        rlEditbarBg=view.findViewById(R.id.rl_editbar_bg);
        bar_edit_text=view.findViewById(R.id.bar_edit_text);
        if(isHidenBarEditTextAndBtn){//隐藏
            bar_edit_text.setVisibility(View.GONE);
            barImageAddBtn.setVisibility(View.GONE);
            btnSend.setVisibility(View.GONE);
            rlEditbarBg.setBackgroundResource(R.color.bg_edittext_color);
        }else{
            bar_edit_text.setVisibility(View.VISIBLE);
            barImageAddBtn.setVisibility(View.VISIBLE);
            btnSend.setVisibility(View.VISIBLE);
            rlEditbarBg.setBackgroundResource(R.drawable.shape_bg_reply_edittext);
        }

    }

    /**
     * 绑定内容View
     * @param contentView
     */
    public void bindToContentView(View contentView){
        mContentView=contentView;
    }

}
